package com.caowei.weicimpress.weicimpress.widget;

import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.caowei.weicimpress.weicimpress.R;

/**
 * Created by caoweiPc on 2014/8/8.
 *
 */
public class SearchWindow {
    private static final String TAG=SearchWindow.class.getSimpleName();
    private ActionBarActivity mActivity;
    private WindowManager mLocalWindowManager;
    private int cover_w;
    private int cover_h;
    private int title_h;
    private View mContainer;
    private View mCover;
    private View mListcontainer;
    private TextView mSearchStr;
    private WindowManager.LayoutParams mParams;
    private boolean isShowing=false;

    private OnAfterCoverTouchListener onAfterCoverTouchListener;

    private OnDismissListener mOnDismissListener;
    public SearchWindow(ActionBarActivity activity){
        this.mActivity=activity;
        mLocalWindowManager=activity.getWindowManager();
        cover_w= activity.getResources().getDisplayMetrics().widthPixels;
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        title_h=mActivity.getSupportActionBar().getHeight()/2;
        cover_h=frame.bottom-(frame.top+title_h);
        mParams=new WindowManager.LayoutParams();
        mParams.type= WindowManager.LayoutParams.TYPE_PHONE;
        mParams.format = PixelFormat.RGBA_8888;
        mParams.flags =   WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
        mParams.gravity = Gravity.LEFT | Gravity.TOP;
        mParams.x=0;
        mParams.y=title_h;
        mParams.width = cover_w;
        mParams.height = cover_h;

        mContainer= LayoutInflater.from(mActivity).inflate(R.layout.window_cover,null);
        mCover=mContainer.findViewById(R.id.cover);
        mListcontainer=mContainer.findViewById(R.id.listcontainer);
        mSearchStr= (TextView) mContainer.findViewById(R.id.searchstr);

        mCover.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                dismiss();
                if(onAfterCoverTouchListener!=null){
                    onAfterCoverTouchListener.onAfter();
                }
                return false;
            }
        });
    }

    public void showListcontainer(boolean show){
        if(mListcontainer.getVisibility()!=View.VISIBLE&&show){
            mCover.setVisibility(View.GONE);
            mListcontainer.setVisibility(View.VISIBLE);
        }else if(mListcontainer.getVisibility()==View.VISIBLE&&!show){
            mCover.setVisibility(View.VISIBLE);
            mListcontainer.setVisibility(View.GONE);
        }
    }

    public void updateListHead(String str){
        mSearchStr.setText(str);
    }

    public void show(){
        if(!isShowing){
            mLocalWindowManager.addView(mContainer, mParams);
            isShowing=true;
        }
    }

    public void dismiss(){
        if(isShowing){
            mLocalWindowManager.removeViewImmediate(mContainer);
            isShowing=false;
            if(mOnDismissListener!=null){
                mOnDismissListener.onDismiss();
            }
        }
    }

    public void setmOnDismissListener(OnDismissListener mOnDismissListener) {
        this.mOnDismissListener = mOnDismissListener;
    }

    public void setSearchClick(View.OnClickListener listener){
        mSearchStr.setOnClickListener(listener);
    }
    public void setOnAfterCoverTouchListener(OnAfterCoverTouchListener onAfterCoverTouchListener) {
        this.onAfterCoverTouchListener = onAfterCoverTouchListener;
    }
    public interface OnDismissListener {
        /**
         * Called when this popup window is dismissed.
         */
        public void onDismiss();
    }

    public interface OnAfterCoverTouchListener {
        /**
         * Called when this popup window is dismissed.
         */
        public void onAfter();
    }

}
